// pages/goods/search/index.js
var app = getApp();
var core = app.requirejs('core');
const api = require("../../../utils/api.js")
Page(app.sr.page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    list: [],
    loaded: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let $this = this;
    $this.setData({
      options
    })
    $this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let _this = this
    console.log(111)
    if (this.data.loaded == true) {
      return
    }
    _this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },

  getList() {
    let $this = this;
    let current = $this.data.current
    let options = $this.data.options
    let apicontent = api.goods.searchGoods()
    core.post(apicontent, {
      keywords: options.value,
      current
    }, function(res) {

      let data = {
        count: res.data.total,
        loadKey: true
      };
      if (res.data.rows.length == 0) {
        res.data.rows = [];
      }
      if (res.data.rows.length > 0) {
        data.current = $this.data.current + 1;
        data.list = $this.data.list.concat(res.data.rows);
        if (res.data.rows.length < res.data.size) {
          data.loaded = true
        }
      }
      $this.setData(data);
    })
  },
}))